<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_language('member', 'login');
		$this->load->module_language('member', 'registration');
		$this->load->module_language('member', 'qlsv');
		$this->load->module_language('member', 'profile');
		$this->load->module_model('member', 'users_model', 'users');
		$this->load->helper(array('form', 'url'));
		$this->load->library('user_agent');
		$this->user = $this->access->get_user();
		$this->load->helper('confirm');
		$this->load->library('session');
		
	}
 
	public function index() 
	{
		$data['ItemID'] = 'User';	
		$this->access->restrict();
		$data['title'] = 'Bảng điều khiển - Quản lý sinh viên trực tuyến';
		$data['description']	=	'';
		$this->_template['page']	= 'users/home';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function login()
	{
		//echo 123;
		//echo 'a'; exit();

		///$this->access->restrict('logged_out');
		if($this->access->logged_in())
		{
			if($this->user->is_admin)
			{
				redirect('admin');
			}
			else
			{
				redirect('member/users');
			}
		}
		$data['ItemID'] = 'User';
		$data['title'] = 'Khu vực đăng nhập hệ thống quản lý';
		$data['description']	=	'Khu vực đăng nhập hệ thống quản lý';
		// Load required files
		$this->load->library('validation');
		$this->load->helper('form');
		$base_url=$this->config->item('base_url');

		$rules = array(
				'username'	=>	'trim|required|strip_tags|xss_clean',
				'password'	=>	'trim|required',
				'token'		=>	'check_login|required'
		);
		
		$this->validation->set_rules($rules);
		
		$fields = array(
				'username'	=>	'username',
				'password'	=>	'password'
		);
		
		$this->validation->set_fields($fields);
		
		if ($this->validation->run() == FALSE)
		{
			$data['token'] = generate_token();
			$this->session->keep_flashdata('referrer');
			$this->_template['page']	= 'users/login';	
			$this->system_library->load($this->_template['page'], $data);
		}
		else
		{
			$uri = $this->session->flashdata('referrer') ? $this->session->flashdata('referrer') : 'member/users/login';
			redirect($uri);
		}
	}

	
	public function logout()
	{
		//Display popup is disable
		
		/*if ($this->user->confirm_logout)
		{
			confirm('Are you sure you want to exit?');
		}*/
		
		$this->access->logout();
		redirect('');
	}
	
	public function register()
	{
		$data['ItemID'] = '';
		$data['title'] = 'Register as Free Member - Here you can Login or Register Member';
		$data['description']	=	'Register as free member, gold member, developer to download free game';
		//$this->load->library('validation');
		$this->load->helper('form');
		$lang_working = load_language_working();
		$this->form_validation->set_rules('username', 'lang:form_username', 'trim|required|strip_tags|xss_clean|max_length[15]|callback_check_username');
		$this->form_validation->set_rules('email', 'lang:form_email', 'trim|required|valid_email|callback_check_email');
		$this->form_validation->set_rules('password','lang:form_password','trim|required');
		$this->form_validation->set_rules('repassword','lang:form_retype_password','trim|required|matches[password]');
		$this->form_validation->set_rules('fullname','lang:form_fullname','required|max_length[50]');
		$this->form_validation->set_rules('address','lang:form_address','max_length[50]');
		$this->form_validation->set_rules('yahoo','lang:form_fullname','max_length[32]');
		$this->form_validation->set_rules('skype','lang:form_skype','max_length[32]');
		$this->form_validation->set_rules('agree','lang:form_agree','required');					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$config['upload_path'] = './uploads/avatars/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1000';
		$config['max_width']  = '800';
		$config['max_height']  = '600';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$data['error'] = ''; //set error when load register
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		if ($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name']=='')
			{	
				$data['error'] = ''; //set error when not choose avatar	
				$userdata = array(
						'username'		=>	$this->input->post('username'),
						'email'			=>	$this->input->post('email'),
						'password'		=>	$this->input->post('password'),
			            'FK_group_id'	=>	$this->input->post('group'),
						'fullname'		=>	$this->input->post('fullname'),
			            'address'		=>	$this->input->post('address'),
			            'yahoo' 		=>	$this->input->post('yahoo'),
			            'skype'		    =>	$this->input->post('skype'),
						'active'		=>	'1',
						);
										
				$register = $this->access->register($userdata);
						
				switch($register)
				{
					case 'REGISTRATION_SUCCESS':
						$this->session->set_flashdata('message', lang('successfully_created'));
						redirect('member/users/login');
					case 'REGISTRATION_SUCCESS_EMAIL':
						$this->session->set_flashdata('message', lang('successfully_created'));
						redirect('');
					default:
						show_error('Registration Failed');
				}
			}
			else
			{
				if(!$this->upload->do_upload())
				{
					//$this->session->set_flashdata('message', $this->upload->display_errors());
					//redirect($this->uri->uri_string());
					//set error when upload file
					$data = array('error' => $this->upload->display_errors());
					$data['ItemID'] = '';
					$data['title'] = 'Register as Free Member - Here you can Login or Register Member';
					$data['description']	=	'Register as free member, gold member, developer to download free game';
					
				}
				else
				{
					$imgdata = $this->upload->data();			
					$userdata = array(
						'username'		=>	$this->input->post('username'),
						'email'			=>	$this->input->post('email'),
						'password'		=>	$this->input->post('password'),
			            'FK_group_id'	=>	$this->input->post('group'),
						'fullname'		=>	$this->input->post('fullname'),
			            'address'		=>	$this->input->post('address'),
			            'yahoo' 		=>	$this->input->post('yahoo'),
			            'skype'		    =>	$this->input->post('skype'),
			            'avatar'		=>	$imgdata['file_name'],
						'active'		=>	'1',
						);
										
					$register = $this->access->register($userdata);
						
					switch($register)
					{
						case 'REGISTRATION_SUCCESS':
							$this->session->set_flashdata('message', lang('successfully_created'));
							redirect('member/users/login');
						case 'REGISTRATION_SUCCESS_EMAIL':
							$this->session->set_flashdata('message', lang('successfully_created'));
							redirect('');
						default:
							show_error('Register Failed');
					}
				}
			}
			
		}	
		
		$this->_template['page']	= 'users/register';	
		$this->system_library->load($this->_template['page'], $data);		
		
	}
	
	public function check_username()
	{
        if($this->users->check_username())
        {
            $this->form_validation->set_message('check_username','Username already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	public function check_email()
	{
        if($this->users->check_email())
        {
            $this->form_validation->set_message('check_email','Email already exist please choose onther one');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
    public function forgot()
    {
    	
    	$this->load->library('validation');		
		$this->load->helper('form');
		
		$rules['email'] = 'trim|required|email';
		$this->validation->set_rules($rules);
		
		$fields['email'] = 'email';
		$this->validation->set_fields($fields);
		
		$data['ItemID'] = '';
		$data['description'] = 'Khôi phục mật khẩu';
		$data['title'] = 'Khôi phục mật khẩu';
		
		if ($this->validation->run() == FALSE)
		{
			$this->system_library->load('users/forgot',$data);
			
		}
		else
		{//echo 'a'; exit();
			if ( $this->access->forgotten_password($this->input->post('email')) )
			{
				$this->session->set_flashdata('message', 'Mật khẩu mới vừa được gửi tới Email của bạn.');
			}
			else
			{
				$this->session->set_flashdata('message', 'An error occurred.');
			}
			redirect('member/users/login');
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/users.php */